package com.plian.system.service.sys.per;

import com.plian.Tools.CollectionUtil;
import com.plian.Tools.TokenUtil;
import com.plian.Tools.TreeUtli;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.config.exception.MyRuntimeException;
import com.plian.system.dao.sys.per.PerUserDao;
import com.plian.system.dao.sys.per.PerUserOrgDao;
import com.plian.system.dao.sys.user.UserDao;
import com.plian.system.entity.sys.fun.SysFun;
import com.plian.system.mapper.sys.fun.SysFunMapper;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MenuService {
    @Autowired
    PerUserDao perUserDao;
    @Autowired
    PerUserOrgDao perUserOrgDao;
    @Autowired
    UserDao userDao;
    @Resource
    SysFunMapper sysFunMapper;
    public ListResult getMenu(){
        //得到token下的用户Id
        String userId= TokenUtil.getTokenUserId();
        String orgId=TokenUtil.getTokenOrgId();
        ListResult listResult;
        try {
            //得到用户信息
            PageData user=userDao.findbyid(userId);
            /**
             * 判断账号是否为企业账号
             */
            List<PageData> list=new ArrayList<>();
            List<PageData> l1=new ArrayList<>();
            TreeUtli treeUtli=new TreeUtli();
            if("3".equals(user.getString("type"))){
                //如果为企业账号
                PageData p=new PageData();
                p.put("userId",userId);
                p.put("orgId",orgId);
                list=perUserOrgDao.getMenu(p);
            }else{
                //如果为非企业账号
                list=perUserDao.getMenu(userId);
            }
           // List<String> ids = list.stream().filter(m -> "0".equals(m.getString("parentId"))).map(m -> m.getString("id")).collect(Collectors.toList());
            List<String> ids = list.stream().map(m -> m.getString("id")).collect(Collectors.toList());
            //移动菜单如果没有父节点，添加
            List<PageData> pdList = new ArrayList<>();
            List<SysFun> allList = sysFunMapper.selectList(null);
/*            for (PageData pd :list){
                String parentId = pd.getString("parentId");
                List<SysFun> pdS = allList.stream().filter(m -> parentId.equals(m.getParentId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(pdS)){
                    SysFun sysFun = pdS.get(0);
                    String pId = sysFun.getId();
                    if (!ids.contains(pId)){
                        ids.add(pId);
                        PageData pageData = new PageData();
                        pageData.put("id",sysFun.getId());
                        pageData.put("name",sysFun.getName());
                        pageData.put("url",sysFun.getUrl());
                        pageData.put("parentId",sysFun.getParentId());
                        pageData.put("style",sysFun.getStyle());
                        pageData.put("type",sysFun.getType());
                        pageData.put("isConfig",sysFun.getIsConfig());
                        pageData.put("ogderNmber",sysFun.getOrderNumber());
                        pdList.add(pageData);
                    }
                }
            }
            list.addAll(pdList);*/
            l1=treeUtli.NoParentIdtree(list);
            listResult=new ListResult(CommonCode.SUCCESS);
            listResult.setList(l1);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            listResult=new ListResult(CommonCode.ERROR);
        }
        return listResult;
    }

    /**
     * 获取前端路由下的用户列表
     * @param pd
     * @return
     */
    public ObjResult getUrlUser(PageData pd){
        //前端路由
        //得到token下的用户Id
        String userId= TokenUtil.getTokenUserId();
        String orgId=TokenUtil.getTokenOrgId();
        List<PageData> list=new ArrayList<>();
        //得到用户信息
        try {
            PageData user=userDao.findbyid(userId);
            if("3".equals(user.getString("type"))){
                //如果为企业账号
                pd.put("orgId",orgId);
                list=perUserOrgDao.getUrlUser(pd);

            }else{
                //如果为非企业账号
                list=perUserDao.getUrlUser(pd);
            }
            if(list!=null && list.size()>0) {
                list=list.stream().filter(pageData -> !pageData.getString("id").equals(userId)).collect(Collectors.toList());
            }
            PageData p=new PageData();
            p.put("list",list);
            ObjResult objResult=new ObjResult(CommonCode.SUCCESS);
            objResult.setPageData(p);
            return  objResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw  new MyRuntimeException(new ObjResult(CommonCode.ERROR));
        }
    }
}
